package catchData;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;

import util.CatchDataUtil;
import util.Conts;

/**
 * @author suwy                     
 * @date 2014-1-8
 * @todo 抓取药监局数据id列表
 */
public class CatchListYJJ {

	private static final Logger LOG = Logger.getLogger(CatchListYJJ.class);
	private static final int STARTPAGE = 1;
	private static final int ENDPAGE = 11215;
	private static final String BCID = "124356560303886909015737447882";
	private static final String TABLEID = "25";
	private static final String WEBURL = "http://app1.sfda.gov.cn/datasearch/face3/search.jsp?tableId="
			+ TABLEID
			+ "&keyword=&bcId="
			+ BCID
			+ "&tableName=TABLE"
			+ TABLEID
			+ "&viewtitleName=COLUMN167&viewsubTitleName=COLUMN1662,COLUMN1702,COLUMN821&curstart=";

	public static void main(String[] args) {
		CatchListYJJ instance = new CatchListYJJ();
		instance.excute();
	}

	private void excute() {
		HttpClient client = CatchDataUtil.initHttpClient();
		PostMethod postMethod = null;
		String regex = "&Id=([^']*)";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = null;
		HashSet<String> webIds = new HashSet<String>();
		try {
			for (int i = STARTPAGE; i <= ENDPAGE; i++) {
				postMethod = new PostMethod(WEBURL + i);
				CatchDataUtil.setHeaders(postMethod, WEBURL + i,
						CatchDataUtil.getWebSite(WEBURL));
				client.executeMethod(postMethod);
				matcher = pattern.matcher(postMethod.getResponseBodyAsString());
				while (matcher.find()) {
					webIds.add(matcher.group().replaceAll("&Id=", "") + ",\n");
				}
				LOG.info("读取药监局-国产药品页数：" + i);
				postMethod.releaseConnection();// 释放链接
				if (webIds.size() > 100) {
					writeWebSet(webIds);
					webIds.clear();//清空集合
				}
			}
			// 获取所有id的集合
			// String[] ids = cBuilder.toString().substring(1,
			// cBuilder.length()).split(",");

			// Arrays.sort(ids);//排序
			// 对比txt文件中原来的id集合
			// File txtFile = new File(TXT_FODER + "domestic_medicine.txt");
			// InputStreamReader reader = new InputStreamReader(
			// new FileInputStream(txtFile)); // 建立一个输入流对象reader
			// BufferedReader br = new BufferedReader(reader); //
			// 建立一个对象，它把文件内容转成计算机能读懂的语言
			// String line = null;
			// HashSet<String> txtIds = new HashSet<String>();
			// while ((line = br.readLine()) != null) {
			// txtIds.add(br.readLine()); // 一次读入一行数据
			// }
			// br.close();
		} catch (Exception e) {
			LOG.info(e);
		}
	}

	private void writeWebSet(HashSet<String> webIds) {
		File txtFile = new File(Conts.HTM_ROOT_PATH + "domestic_medicine.csv");
		FileWriter fw = null;
		try {
			fw = new FileWriter(txtFile,true);
			for (Iterator<String> iterator = webIds.iterator(); iterator.hasNext();) {
				fw.append(iterator.next());
			}
			fw.flush();
        } catch (IOException e) {
			LOG.info(e);
		} finally {
			try {
				fw.close();
			} catch (IOException e) {
				LOG.info(e);
			}
		}
	}
}